#include "PieceWidget.hpp"
#include "../Chess.hpp"
#include "View.hpp"
#include "ChessGame.hpp"
#include "LinkManager.hpp"
#include "StyleManager.hpp"

#define PIECE_WIDTH		30
#define PIECE_HEIGHT	30

#define PIECE_XOFFSET	0
#define PIECE_YOFFSET	0


PieceWidget::PieceWidget(Piece* p)
	: StaticSpriteWidget(new StaticSprite(StyleManager::getInstance()->getStyle()->getPiecePixmap(p->getColor(), p->getType(), Style::P_STANDARD),
										  ChessGame::getInstance()->getView()->getScene()
								          )
						), piece(p), connected(false), activatedSelect(false), activatedChoice(false)
{
	this->setTransparent(true);
}

PieceWidget::~PieceWidget()
{}


void	PieceWidget::setActivatedAsSelect(bool val)
{
	if (val)
	{
		if (!activatedSelect)
		{
			connect(this, SIGNAL(clicked()), LinkManager::getInstance(), SLOT(pieceSelect()));
			activatedSelect = true;
		}
	}
	else
	{
		this->spr->setPixmap(StyleManager::getInstance()->getStyle()->getPiecePixmap(piece->getColor(), piece->getType(), Style::P_STANDARD));
		
		if (activatedSelect)
		{
			disconnect(this, SIGNAL(clicked()), LinkManager::getInstance(), SLOT(pieceSelect()));
			activatedSelect = false;
		}
	}
}

void	PieceWidget::setActivatedAsChoice(bool val)
{
	if (val)
	{	
		this->spr->setPixmap(StyleManager::getInstance()->getStyle()->getPiecePixmap(piece->getColor(), piece->getType(), Style::P_CHOICE));

		if (!activatedChoice)
		{
			connect(this, SIGNAL(clicked()), LinkManager::getInstance(), SLOT(pieceChoice()));
			activatedChoice = true;
		}
	}
	else
	{
		this->spr->setPixmap(StyleManager::getInstance()->getStyle()->getPiecePixmap(piece->getColor(), piece->getType(), Style::P_STANDARD));

		if (activatedChoice)
		{
			disconnect(this, SIGNAL(clicked()), LinkManager::getInstance(), SLOT(pieceChoice()));
			activatedChoice = false;
		}
	}
}

void PieceWidget::setPixmapSelected(bool val)
{
	if (val)
		this->spr->setPixmap(StyleManager::getInstance()->getStyle()->getPiecePixmap(piece->getColor(), piece->getType(), Style::P_SELECTED));
	else
		this->spr->setPixmap(StyleManager::getInstance()->getStyle()->getPiecePixmap(piece->getColor(), piece->getType(), Style::P_STANDARD));
}

Piece*	PieceWidget::getPiece() const
{	return piece;	}

void	PieceWidget::refresh()
{		
	Position pos = piece->getCell()->getPosition();
			
	this->setPos(PIECE_XOFFSET + pos.getRealX() * PIECE_WIDTH, 
				PIECE_YOFFSET + 210 -(pos.getRealY() * PIECE_HEIGHT));
				
	this->show();
}

